//
//  HBHMyViewController.m
//  HubeiHotel
//
//  Created by 张露杰 on 17/11/13.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHMyViewController.h"
#import "MyHeaderView.h"
#import "MyCell.h"
#import "SetViewController.h"
#import "MessageViewController.h"
#import "MyPostViewController.h"
#import "AttentionViewController.h"
#import "FansViewController.h"
#import "MyCollectionViewController.h"
#import "MyLookedViewController.h"
#import "ResumeViewController.h"
#import "PositionViewController.h"
#import "DiscountsViewController.h"
#import "MyAboutViewController.h"
#import "MyServiceViewController.h"
#import "MyPartentViewController.h"
static NSString *const MyCellID = @"MyCellID";
@interface HBHMyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSArray *titleImageArr;//图片数组
@property (nonatomic,strong)UIView *footView;//区尾
@property (nonatomic,strong) MyHeaderView *headerView;//headerView信息view
@property (nonatomic,strong)MyHeaderView *cellView;
@end

@implementation HBHMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    //    初始化变量
    _titleImageArr = @[@[@"我的收藏",@"soucang_icon"],
                       @[@"我浏览过的",@"liulan_icon"],
                       @[@"我的简历",@"jianli_icon"],
                       @[@"投递的职位",@"zhiwei_icon"],
                       @[@"我的优惠",@"youhuijuan_icon"],
                       @[@"关于湖北网",@"about_icon"],
                       @[@"客服中心",@"kefu_icon"],
                       @[@"我要合作",@"hezuo_icon"]];
    [self.view addSubview:self.tableV];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    [self.tableV reloadData];
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,-24, SCREENW, SCREENH-49+24) style:UITableViewStyleGrouped];
        _tableV.rowHeight = 50*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [_tableV registerClass:[MyCell class] forCellReuseIdentifier:MyCellID];
    }
    return _tableV;
}

#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _titleImageArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 214*screenW_6;
    }else
    {
        return 10*screenW_6;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80*screenW_6;
    }
    return 50*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //加载cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (!_cellView) {//个人中心区头
           _cellView = [[MyHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 80*screenW_6)];
            [_cellView addThreeBtn];
             __weak typeof(self)weakSelf = self;
            _cellView.headerBlock = ^(NSUInteger tagNumner){
                if (tagNumner == 0) {//我的帖子
                    [weakSelf.navigationController pushViewController:[MyPostViewController new] animated:YES];
                }else if (tagNumner == 1){//关注
                    [weakSelf.navigationController pushViewController:[AttentionViewController new] animated:YES];
                }else if (tagNumner == 2){//粉丝
                    [weakSelf.navigationController pushViewController:[FansViewController new] animated:YES];
                }
            };
        }
        [cell addSubview:_cellView];
        return cell;

    }else{
        //加载cell
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setTitleStr:_titleImageArr[indexPath.row][0]];
        [cell setHeaderStr:_titleImageArr[indexPath.row][1]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;

    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (!_headerView) {//个人中心区头
            _headerView = [[MyHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 214*screenW_6)];
            [_headerView CreatUI];
            __weak typeof(self)weakSelf = self;
            _headerView.headerBlock = ^(NSUInteger tagNumner){
                if (tagNumner == 10) {//我的信息
                    [weakSelf.navigationController pushViewController:[MessageViewController new] animated:YES];
                }else if (tagNumner == 11){//设置
                    [weakSelf.navigationController pushViewController:[SetViewController new] animated:YES];
                }
            };
            
            _headerView.changeHeaderBlock = ^(){
                [weakSelf changeHead];
            };
        }
        [_headerView setDataDic:nil];
        return _headerView;
    }
    return nil;
  
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0://我的收藏
        {
            [self.navigationController pushViewController:[MyCollectionViewController new] animated:YES];
        }
            break;
        case 1://我的浏览
        {
           [self.navigationController pushViewController:[MyLookedViewController new] animated:YES];
        }
            break;
        case 2://我的简历
        {
           [self.navigationController pushViewController:[ResumeViewController new] animated:YES];
        }
            break;
        case 3://投递的职位
        {
          [self.navigationController pushViewController:[PositionViewController new] animated:YES];        }
            break;
        case 4://我的优惠
        {
           [self.navigationController pushViewController:[DiscountsViewController new] animated:YES];
        }
            break;
        case 5://关于
        {
           [self.navigationController pushViewController:[MyAboutViewController new] animated:YES];
        }
            break;
        case 6://客服中心
        {
            [self.navigationController pushViewController:[MyServiceViewController new] animated:YES];
        }
            break;
            
        case 7://我要合作
        {
            [self.navigationController pushViewController:[MyPartentViewController new] animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {//监测偏移量 改变headerBackView的Frame
    CGFloat yOffset = scrollView.contentOffset.y;
    if (yOffset < 0) {
        CGFloat totalOffset = 214 * screenW_6 + ABS(yOffset);
        self.headerView.backmageV.frame = CGRectMake(0, yOffset,SCREENW, totalOffset);
    }
}

-(void)changeHead
{
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
        HWDUpALIPhoto *ALIPhoto = [[HWDUpALIPhoto alloc]initWithImages:photos success:^(NSArray *imageNames) {
            [self upHeadWithUrl:imageNames[0]];
        } fail:^{//上传失败
            [HudHelper hideHudToView:self.view];
            [HudHelper showHudWithMessage:@"上传失败，请重试" toView:self.view];
        }];
    }];
    [[TomamaUI getTopVC] presentViewController:imagePickerVc animated:YES completion:nil];
}

-(void)upHeadWithUrl:(NSString *)url
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/login/updateUserInfo" Dic:@{@"id":[Storage getToken],@"headPortrait":url} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:[standfault objectForKey:@"userInfo"]];
                    [userInfo setObject:url forKey:@"headPortrait"];
                    [standfault setObject:userInfo forKey:@"userInfo"];
                    [standfault synchronize];
                    [TomamaUI creatAlertWithTarget:self message:@"发布成功" timeInterval:2];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
